package com.jeeplus.config;

import com.jeeplus.core.persistence.Page;
import com.jeeplus.database.persistence.DsAjaxJson;
import com.jeeplus.database.persistence.DsBaseController;
import com.jeeplus.database.persistence.DsStringUtils;
import com.jeeplus.modules.gencode.entity.A12;
import com.jeeplus.modules.gencode.entity.A14;
import com.jeeplus.modules.gencode.entity.A19;
import com.jeeplus.modules.gencode.entity.A5;
import com.jeeplus.modules.gencode.mapper.A12Mapper;
import com.jeeplus.modules.gencode.mapper.A14Mapper;
import com.jeeplus.modules.gencode.mapper.A15Mapper;
import com.jeeplus.modules.gencode.mapper.A6Mapper;
import com.jeeplus.modules.gencode.service.*;
import org.apache.commons.lang.StringUtils;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Component;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Iterator;
import java.util.List;

@Aspect
@Component
public class CrackLicenseAOP extends DsBaseController {

    @Autowired
    public B9Service b9Service;
    @Autowired
    public B8Service b8Service;
    @Autowired
    public B6Service genSchemeService;
    @Autowired
    public A15Mapper a15Mapper;
    @Autowired
    private A14Mapper a14Mapper;
    @Autowired
    private A12Mapper a12Mapper;
    @Autowired
    private A6Mapper a6Mapper;
    @Autowired
    private B3Service b3Service;
    @Autowired
    private B2Service b2Service;
    @Autowired
    private JdbcTemplate jdbcTemplate;

    public A14 get(A14 a14) {
        return DsStringUtils.isNotBlank(a14.getId()) ? this.b8Service.get(a14.getId()) : a14;
    }

    private A14 getDataSource(A14 a14) {
        String sql = "select * from plugin_datasource_link a where a.id='" + a14.getDataSource().getId() + "'";
        A19 dataSource = (A19) this.jdbcTemplate.queryForObject(sql, new BeanPropertyRowMapper(A19.class));
        a14.setDataSource(dataSource);
        return a14;
    }

    @Around("execution(com.jeeplus.database.persistence.DsAjaxJson com.jeeplus.modules.gencode.web.D8.data(..))")
    public DsAjaxJson dataAround(ProceedingJoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        A14 a14 = (A14) args[0];
        Page<A14> page = new Page<>((HttpServletRequest) args[1], (HttpServletResponse) args[2]);
        page = (Page<A14>) b8Service.find(page, a14);
        return DsAjaxJson.success().put("page", page);
    }

    @Around("execution(com.jeeplus.database.persistence.DsAjaxJson com.jeeplus.modules.gencode.web.D8.save(..))")
    public DsAjaxJson saveAround(ProceedingJoinPoint joinPoint) {
        Object[] args = joinPoint.getArgs();
        A14 a14 = (A14) args[0];
        A14 oldA14 = this.get(a14);
        a14.setOldComments(oldA14.getOldComments());
        a14.setOldName(oldA14.getOldName());
        a14.setOldGenIdType(oldA14.getOldGenIdType());
        a14 = this.getDataSource(a14);
        DsAjaxJson j = new DsAjaxJson();
        String errMsg = this.beanValidator(a14, new Class[0]);
        if (DsStringUtils.isNotBlank(errMsg)) {
            j.setSuccess(false);
            j.setMsg(errMsg);
            return j;
        } else if (DsStringUtils.isBlank(a14.getId()) && !this.b8Service.checkTableName(a14)) {
            j.setSuccess(false);
            j.setMsg("添加失败！" + a14.getName() + " 记录已存在！");
            return j;
        } else if (DsStringUtils.isBlank(a14.getId()) && !this.b8Service.checkTableNameFromDB(a14)) {
            j.setSuccess(false);
            j.setMsg("添加失败！" + a14.getName() + "表已经在数据库【" + a14.getDataSource().getName() + "】中存在,请从数据库导入表单！");
            return j;
        } else if (DsStringUtils.isNotBlank(a14.getId()) && !a14.getName().equals(oldA14.getName()) && !this.b8Service.checkTableName(a14)) {
            j.setSuccess(false);
            j.setMsg("重命名失败！" + a14.getName() + " 记录已存在！");
            return j;
        } else if (DsStringUtils.isNotBlank(a14.getId()) && !a14.getName().equals(oldA14.getName()) && !this.b8Service.checkTableNameFromDB(a14)) {
            j.setSuccess(false);
            j.setMsg("重命名失败！" + a14.getName() + "表已经在数据库【" + a14.getDataSource().getName() + "】中存在,请从数据库导入表单！");
            return j;
        } else {
            List<A12> columns = a14.getColumnList();
            Iterator var10 = columns.iterator();

            while (var10.hasNext()) {
                A12 column = (A12) var10.next();
                if (StringUtils.isNotBlank(column.getId())) {
                    A12 old = (A12) this.a12Mapper.get(column.getId());
                    column.setOldIsPk(old.getOldIsPk());
                    column.setOldJdbcType(old.getOldJdbcType());
                    column.setOldName(old.getOldName());
                    column.setOldComments(old.getOldComments());
                }

                A5 genCuObj;
                if ("gridselect".equals(column.getShowType())) {
                    if (!"This".equals(column.getJavaType())) {
                        genCuObj = (A5) this.b3Service.findUniqueByProperty("value", column.getJavaType());
                        if (genCuObj == null) {
                            j.setSuccess(false);
                            j.setMsg(column.getJavaField() + "字段对应的显示表单类型你选择了【gridselect】或者【树选择控件】，<br/>但是对应的java类型你没有关联自定义java对象！<br/>");
                            return j;
                        }

                        column.setDataUrl(genCuObj.getDataUrl());
                        column.setTableName(genCuObj.getTableName());
                    }
                } else if ("treeselect".equals(column.getShowType()) && !"This".equals(column.getJavaType()) && !column.getName().equals(a14.getParentTableFk())) {
                    genCuObj = (A5) this.b3Service.findUniqueByProperty("value", column.getJavaType());
                    if (genCuObj == null) {
                        j.setSuccess(false);
                        j.setMsg(column.getJavaField() + "字段对应的显示表单类型你选择了【gridselect】或者【树选择控件】，<br/>但是对应的java类型你没有关联自定义java对象！<br/>");
                        return j;
                    }

                    column.setDataUrl(genCuObj.getDataUrl());
                    column.setTableName(genCuObj.getTableName());
                }
            }
            if (DsStringUtils.isNotBlank(a14.getOldName()) && !this.b8Service.checkTableNameFromDB(a14)) {
                this.b8Service.save(a14);
            } else {
                this.b8Service.saveNewTable(a14);
            }
            j.setSuccess(true);
            j.setMsg("保存业务表'" + a14.getName() + "'成功");
            return j;
        }
    }
}
